from imports import *
class ui_Reptil:                     # 设计爬虫窗口的UI界面
    W = 800
    H = 600
    urlList = ['https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2926199504,3558053336&fm=26&gp=0.jpg',
       'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1353199357,284990746&fm=26&gp=0.jpg',
       'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ftu1.whhost.net%2Fuploads%2F20181112%2F15%2F1542007027-liatSvAxeE.jpg&refer=http%3A%2F%2Ftu1.whhost.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625977897&t=788ae3ae24176bd586298a6ddf6098d7']
    def __init__(self, frame):
        W, H = self.W, self.H               # 初始化窗口大小
        self.K = 0                          # 图片轮播计数
        self.frame = frame                  # 保存成员变量窗口frame

        self.image = tk.Label(self.frame, width = W, height = H)        # 创建图片标签
        self.image.place(x = W / 4, y = H / 10)                         # 设置坐标

        self.b_more = tk.Button(self.frame, width=10, height=2, text='更多信息', font=('幼圆', 13), command=self.toNextImage)     # 创建按钮标签, 并设置响应方法为toNextImage
        self.b_more.place(x=W - 300, y= H - 100)                                                                                  # 设置坐标
    def setImage(self,url, x = W, y = H):   # 设置标签图片
        try:
            image_bytes = urlopen(url).read()           # 1. 获取 图片的二进制数据
            data_stream = io.BytesIO(image_bytes)       # 2. # 创建图片对于的IO流对象
            pil_image = Image.open(data_stream).resize((x, y), Image.ANTIALIAS) # 3. 根据IO流对象打开图片地址并拉伸图片大小为x，y
            self.tk_image = ImageTk.PhotoImage(pil_image)   # 4. 保存图片信息到Tk组件
            self.image.configure(width = x, height = y, image = self.tk_image) # 5. 设置组件信息, 会自动刷新
            return True                                 # 操作成功返回True
        except:
            return False                                # 若 try失败，则说明url有误，返回False

    def toNextImage(self):                              # 计算图片的下一个位置
        self.K = (self.K + 1) % len(self.urlList)
        self.setImage(self.urlList[self.K], 400, 300)   # 设置标签图片、并指定图片大小

    def initButton(self):
        pass
